<?php
include ("includes/header.php");
include ("includes/connection.php");
include ("includes/login.php");
include ("includes/iniciaSessio.php");
?>
<body>
	<?php
	include ("./includes/contentHeader.php");
	?>
	<!--contingut 1-->
	<section class="container">
		<!--fons gris-->
		<div class="hero-unit ">
			<h2> Perfil d'usuari </h2>
			<br>
			<?php
			$alias=$_SESSION["username"];
			$sql="SELECT * FROM usuari where alias ='$alias'";
			$res=mysql_query($sql);
			if (!$res) {
			$missatge = 'Consulta Invalida: ' . mysql_error() . "\n";
			$missatge .= 'Consulta SQL: ' . $sql;
			}
			if ($row=mysql_fetch_array($res))
			{
			?>
			<section class="row show-grid ">
				<div class="span3">
					<img src="<?php echo $row["foto"];?>" alt="foto" height="200" width="200"/>
					<hr>
					<form method="post" enctype="multipart/form-data" action="perfil_usuari.php">
						Selecciona la foto:
						<input type="file" name="foto" value="foto" class="btn btn-success">
						<input type="Submit" class="btn btn-primary" name="modificarfoto" value="Modificar foto">
					</form>
				</div>
				<div class="span3">
					<label>Nom: <?php echo " " . $row["nomUsuari"];?></label>
					<label><?php echo "Cognoms: " . $row["cognom1"] . " " . $row["cognom2"];?></label>
					<label><?php echo "Alias: " . $row["alias"];?></label>
					<label> <?php echo "Sexe: " . $row["sexe"];?></label>
					<label> <?php echo "Data de naixement: " . $row["dataNaixement"];?></label>
					<label> <?php echo "Biografia: " . $row["biografia"];?></label>
				</div>
				<div class="span3">
					<form method="post" action="perfil_modificar.php">
						<input type="Submit" name="modificar" value="Editar el teu perfil" class="btn btn-primary">
					</form>
					<div/>
					</button>
					<button class="btn btn-primary" name="insert" type="submit" value="Modificar contrasenya" data-toggle="modal" onmousedown="location.href = 'modificar_contrasenya.php'";>
						Modificar contrasenya
					</button>
				</div>
				<br>
				<a href="index.php"><img src="images/home.png" title="Tornar al principi" alt="Tornar al principi" width="150" height="150"></a><?php

				//modificar foto por acabar
				mysql_select_db("sintesi", $conexion);
				//$foto = $_POST['foto'];
				$alias = $_SESSION["username"];
				$sql = "SELECT * FROM usuari where alias ='$alias'";
				$res = mysql_query($sql);
				if (!$res) {
					$missatge = 'Consulta Invalida: ' . mysql_error() . "\n";
					$missatge .= 'Consulta SQL: ' . $sql;
				}

				if (empty($_POST['foto']) and empty($_POST['modificarfoto'])) {

				} else {
					$archivo = $_FILES['foto']['name'];
					if ($archivo != "") {
						$destino = "images/" . $archivo;
						//variable foto
						copy($_FILES['foto']['tmp_name'], $destino);

						$consulta = "UPDATE usuari SET foto = '$destino' WHERE alias = '$alias'";

						$result = (mysql_query($consulta, $conexion));

						if ($result) {
							echo "
<script>
alert('Foto canviada');
</script>
";
							header("refresh:0; url=perfil_usuari.php");
						} else {
							echo "
<script>
alert('Error al canviar la foto');
</script>
";
						}

					}
				}

				echo "</section>";

				}
				?>
		</div>
		<!-- fi fons gris-->
	</section>
	<!-- fi contingut 1-->
	<!--contingut 2-->
	<section class="container">
		<div class="hero-unit">
			<form class="form-inline" action="#" method="post">
				<h2>Els meus Vehicles</h2>
				<br>
				<?php
$alias = $_SESSION["username"];
$sql="select marca.nom, vehicle.model, vehicle.matricula, usuari.alias, vehicle.idVehicle from (vehicle join marca on vehicle.idMarca=marca.idMarca)join usuari on vehicle.idUsuari=usuari.idUsuari where  usuari.alias='$alias' and vehicle.eliminat='no'";
//$sql = "select marca.nom, vehicle.model, vehicle.matricula, usuari.alias, vehicle.idVehicle from vehicle, marca, usuari where vehicle.idMarca=marca.idMarca and usuari.idUsuari = vehicle.idUsuari and usuari.alias='$alias' and vehicle.eliminat='no'";
$result = mysql_query($sql, $conexion) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$idVehicles=$row['idVehicle'];
echo ($row['nom'] .  " - " . $row['model'] .  " - " . $row['matricula'] . "
");
				?>
				<button class="enviarFrm btn btn-danger" name="eliminar" type="submit" value="Eliminar coche" >
					Eliminar
				</button>
				<br>
				<br>
				<?php
				}
				if (isset($_POST['eliminar'])) {

				$user=$_SESSION["username"];
				# Possem el coche eliminat com a eliminat = "si".
				$sql = "UPDATE vehicle, usuari SET vehicle.eliminat='si' where vehicle.idUsuari = usuari.idUsuari and alias='$user' and vehicle.idVehicle='$idVehicles'";
				if (mysql_query($sql, $conexion)) {
				echo "
				<script>
				alert('Coche eliminat correctamente.');				</script>
				";
				header("refresh:0; url=perfil_usuari.php");
				} else {
				echo "
				<script>
				alert('Error al eliminar el coche.');
				</script>
				";
				}
				}
				?>
				<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">
					Afegir Vehicle
				</button>
			</form>
			<!--finestre modal-->
			<div  id="myModal" class="modal hide fade">
				<div class="modal-header">
					<a class="close" data-dismiss="modal">×</a>
					<h2>Nou cotxe</h2>
				</div>
				<div class="modal-body">
					<form class="form-inline" action="perfil_usuari.php" method="post">
						<div class="control-group">
							<label class="control-label"  for="select">Marca:</label>
							<div class="controls">
								<?php
								$sql = "select * from marca group by idMarca ASC";
								$result = mysql_query($sql, $conexion) or die(mysql_error());
								?>
								<select name="marca" id="select"  class="input-medium">
									<?php
									while ($row = mysql_fetch_array($result)) {
										echo "<option value=" . $row['idMarca'] . ">" . $row['nom'] . "</option>";
									}
									?>
								</select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label"  for="model">Model:</label>
							<div class="controls">
								<input class="input-medium" name="model" type="text" />
							</div>
						</div>
						<div class="control-group">
							<label class="control-label"  for="matricula">Matricula:</label>
							<div class="controls">
								<input class="input-medium" name="matricula" type="text" />
							</div>
						</div>
						<!--<div class="control-group">
						<label class="control-label"  for="select">Combustible:</label>
						<div class="controls">
						<select name="combustible" id="select"  class="input-medium">
						<option value="g">Benzina</option>
						<option value="d">Diesel</option>
						<option value="h">Híbrid</option>
						</select>
						</div>
						</div>-->
						</br></br> <a class="enviarFrm btn btn-danger" data-dismiss="modal">Cancel·lar</a>
						<button class="enviarFrm btn btn-success" name="insert" type="submit" value="Afegir vehicle">
							Afegir vehicle
						</button>
					</form>
				</div>
				<!--<div class="modal-footer">
				<a href="#" class="btn">Close</a>
				<a href="#" class="btn btn-primary">Save changes</a>
				</div>-->
				<?php
				if (isset($_POST['insert'])) {
					if (isset($_POST['marca']) && isset($_POST['model']) && isset($_POST['matricula'])) {
						$idUsuari = $_SESSION["idUsuari"];
						$marca = $_POST['marca'];
						$model = $_POST['model'];
						$matricula = $_POST['matricula'];
						echo($marca);
						echo($model);
						echo($matricula);
						echo(idTipusVehicle);
						echo(idUsuari);
						$sql = "INSERT INTO vehicle (idMarca,model,idUsuari,matricula,idTipusVehicle) VALUES ('$marca','$model','$idUsuari','$matricula','1')";
						if (mysql_query($sql, $conexion)) {
							echo "
<script>
alert('Coche afegit correctament') ;</script>
";
							header("refresh:1; url=perfil_usuari.php");
						} else {
							echo "
<script>
alert('Error al afegir coche');
</script>
";
						}
					}
				}
				?>
			</div><!--fi finestra-->
		</div>
	</section>
	<!-- fi contingut 2-->
	<hr>
	<?php
	include ("includes/footer.php");
	?>
